# 多标量梯度计算
import torch


def _test():
    x1 = torch.tensor(1.0, requires_grad=True,dtype=torch.float32)
    x2 = torch.tensor(2.0, requires_grad=True,dtype=torch.float32)
    y = x1 ** 2 + x2 ** 2 + 6 * x1 + 3
    y.backward()
    print(x1.grad)
    # 对x1求导
    print(x2.grad)
    # 对x2求导
if __name__ == '__main__':
    _test()